﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ZofX.NpoiXHelper.Lib
{
    /// <summary>
    /// 读取与设置.NET应用程序的config配置文件
    /// </summary>
    public class AppConfigHelper
    {
        /// <summary>
        /// Xml文档对象
        /// </summary>
        XmlDocument xDoc = new XmlDocument();

        /// <summary>
        /// Xml节点对象
        /// </summary>
        XmlNode xNode;

        /// <summary>
        /// Xml元素对象
        /// </summary>
        XmlElement xElem;

        /// <summary>
        /// config文件路径
        /// </summary>
        string AppConfigPath;

        /// <summary>
        /// 构造函数
        /// </summary>
        public AppConfigHelper(string appConfigPath)
        {
            AppConfigPath = appConfigPath;
            xDoc.Load(AppConfigPath);
        }

        /// <summary>
        /// 设置appSettings键
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        public void SetAppSettings(string key, string val)
        {
            xNode = xDoc.SelectSingleNode("//appSettings");
            xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
            xElem.SetAttribute("value", val);
            xDoc.Save(AppConfigPath);
        }

        /// <summary>
        /// 读取appSettings键
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public string GetAppSettings(string key)
        {
            try
            {
                xNode = xDoc.SelectSingleNode("//appSettings");
                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
                if (xElem != null)
                    return xElem.GetAttribute("value");
                else
                    return "";
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 设置connectionStrings键
        /// </summary>
        /// <param name="name">键</param>
        /// <param name="value">值</param>
        public void SetConnectionStrings(string name, string value)
        {
            xNode = xDoc.SelectSingleNode("//connectionStrings");
            xElem = (XmlElement)xNode.SelectSingleNode("//add[@name='" + name + "']");
            xElem.SetAttribute("connectionString", value);
            xDoc.Save(AppConfigPath);
        }

        /// <summary>
        /// 读取connectionStrings键
        /// </summary>
        /// <param name="name">键</param>
        /// <returns>值</returns>
        public string GetConnectionStrings(string name)
        {
            try
            {
                xNode = xDoc.SelectSingleNode("//connectionStrings");
                xElem = (XmlElement)xNode.SelectSingleNode("//add[@name='" + name + "']");
                if (xElem != null)
                    return xElem.GetAttribute("connectionString");
                else
                    return "";
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 设置某元素某属性值
        /// </summary>
        /// <param name="xpath">xPath路径</param>
        /// <param name="elementName">元素名</param>
        /// <param name="getAttrName">获取指定元素的属性名称</param>
        /// <param name="getAttrValue">获取指定元素的属性值</param>
        /// <param name="setAttrName">设置指定元素的属性名称</param>
        /// <param name="setAttrValue">设置指定元素的属性值</param>
        public void SetNodeAttr(string xpath, string elementName,
            string getAttrName, string getAttrValue,
            string setAttrName, string setAttrValue)
        {
            xNode = xDoc.SelectSingleNode(xpath);
            xElem = (XmlElement)xNode.SelectSingleNode(string.Format("//{0}[@{1}='{2}']", elementName, getAttrName, getAttrValue));
            xElem.SetAttribute(setAttrName, setAttrValue);
            xDoc.Save(AppConfigPath);
        }

        /// <summary>
        /// 获取某元素某属性值
        /// </summary>
        /// <param name="xpath">xPath路径</param>
        /// <param name="elementName">元素名</param>
        /// <param name="getAttrName">获取指定元素的属性名称</param>
        /// <param name="getAttrValue">获取指定元素的属性值</param>
        /// <param name="returnAttrName">返回指定元素的属性名称</param>
        public string GetNodeAttr(string xpath, string elementName,
            string getAttrName, string getAttrValue,
        string returnAttrName)
        {
            try
            {
                xNode = xDoc.SelectSingleNode(xpath);
                xElem = (XmlElement)xNode.SelectSingleNode(string.Format("//{0}[@{1}='{2}']", elementName, getAttrName, getAttrValue));
                if (xElem != null)
                    return xElem.GetAttribute(returnAttrName);
                else
                    return "";
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 设置节点值
        /// </summary>
        /// <param name="xpath">XPath路径</param>
        /// <param name="val">值</param>
        public void SetNodeValue(string xpath, string val)
        {
            xNode = xDoc.SelectSingleNode(xpath);
            xElem = (XmlElement)xNode;
            xElem.InnerText = val;
            xDoc.Save(AppConfigPath);
        }

        /// <summary>
        /// 读取节点值
        /// </summary>
        /// <param name="xpath">XPath路径</param>
        /// <returns>值</returns>
        public string GetNodeValue(string xpath)
        {
            try
            {
                xNode = xDoc.SelectSingleNode(xpath);
                xElem = (XmlElement)xNode;
                if (xElem != null)
                    return xElem.InnerText;
                else
                    return "";
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 获取子节点
        /// </summary>
        /// <param name="xpath">xPath路径</param>
        /// <returns>子节点列表</returns>
        public XmlNodeList GetChildNodes(string xpath)
        {
            //xNode = xDoc.SelectSingleNode(xpath);
            XmlNodeList nodes = xDoc.SelectNodes(xpath);
            if (null != nodes)
            {
                return nodes;
            }
            return null;
        }
    }
}